import { segment } from "oicq";
import fetch from "node-fetch";
import fs from "fs";
import common from'../../lib/common/common.js'

var dirpath = "resources/1";//路径
if (!fs.existsSync(dirpath)) {
  fs.mkdirSync(dirpath);}
  var filename = "1.json";//文件名
  if (!fs.existsSync(dirpath+"/"+filename)) {
    fs.appendFileSync(dirpath + "/" + filename, JSON.stringify({"num": 0}))
      
      // 随机生成1-10000之间的数字
      let randomNum = Math.floor(Math.random() * 10000 + 1);
      let a = '{"num": '+randomNum+'}'
      let jsonData = JSON.parse(a);
      // 写入文件
      fs.writeFile(dirpath + "/" + filename, JSON.stringify(jsonData), (err) => {
        if (err) {
          console.log(err);
        }
      })}

//
let timer = setInterval(() => {if (!fs.existsSync(dirpath)) {
  fs.mkdirSync(dirpath);
  }
  var filename = "1.json";//文件名
  if (!fs.existsSync(dirpath+"/"+filename)) {
    fs.appendFileSync(dirpath + "/" + filename, JSON.stringify({"num": 0}))
    } else {
      
      // 随机生成1-10000之间的数字
      let randomNum = Math.floor(Math.random() * 10000 + 1);
      let a = '{"num": '+randomNum+'}'
      let jsonData = JSON.parse(a);
      // 写入文件
      fs.writeFile(dirpath + "/" + filename, JSON.stringify(jsonData), (err) => {
        if (err) {
          console.log(err);
        }
      });
      console.log(randomNum);
    }
    },1800000);
    let jiuji
    let tname
    

export class example extends plugin {
  constructor() {
    super({
      /** 功能名称 */
      name: '111',
      /** 功能描述 */
      dsc: '111',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 40,
      rule: [ 
         {
        /** 命令正则匹配 */
        reg: "^#炒股帮助",
        /** 执行方法 */
        fnc: 'help'
      },
        {
          /** 命令正则匹配 */
          reg: "^#查询股价",
          /** 执行方法 */
          fnc: 'text'
        },
        {
          /** 命令正则匹配 */
          reg: "^#我的账户",
          /** 执行方法 */
          fnc: 'wdzh'
        },
        {
          /** 命令正则匹配 */
          reg: "^#买入股票(.*)$",
          /** 执行方法 */
          fnc: 'buy'
        },
        {
          /** 命令正则匹配 */
          reg: "^#卖出股票(.*)$",
          /** 执行方法 */
          fnc: 'sell'
        },
        {
          /** 命令正则匹配 */
          reg: "^#领取救济",
          /** 执行方法 */
          fnc: 'lqjj'
        },
        {
          /** 命令正则匹配 */
          reg: "^#t",
          /** 执行方法 */
          fnc: 't'
           /** 开发者测试用，可忽略 */
        },
      

      ]
    })
  }

  //执行方法



  async text(e) { var filename = "1.json"
    var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
    let i = ress.replace(/[^0-9]/ig,'')    
     e.reply('现在的股价为:'+i+',你可以选择买入股票或卖出股票')   
    };


    async wdzh(e) { 
      
let name = e.user_id
let filename2 =  `${name}.json`
let name1 = e.user_id+'1'
let filename1 =  `${name1}.json`
if (!fs.existsSync(dirpath+"/"+filename2)) {fs.appendFileSync(dirpath + "/" + filename2, JSON.stringify({"num": 0}))
                                            let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
                                            let y = x.replace(/[^0-9]/ig,'') 
                                            e.reply('你的账户余额为：'+y+',持有股为0股，回复 #领取救济 领点钱吧！')
    }else{let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
    let y = x.replace(/[^0-9]/ig,'') 
    if (!fs.existsSync(dirpath+"/"+filename1)) {
      fs.appendFileSync(dirpath + "/" + filename1, JSON.stringify({"num": 0}))
      let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
      let y = x.replace(/[^0-9]/ig,'') 
      e.reply('你的账户余额为：'+y+',持有股为:0股，回复 #领取救济 领点钱吧！')
    }else
    {
    let x1 = fs.readFileSync(dirpath + "/" + filename1,"utf8")
    let y1 = x1.replace(/[^0-9]/ig,'') 
    e.reply('你的账户余额为：'+y+',持有股为：'+y1+'股')}}

}
async buy(e) {
  let name = e.user_id
  let filename2 =  `${name}.json`
   if (!fs.existsSync(dirpath+"/"+filename2)) {
    fs.appendFileSync(dirpath + "/" + filename2, JSON.stringify({"num": 0}))
    e.reply('你的账户里一分钱也没有，回复 #领取救济 领点钱吧！')}else{
 let msg = e.msg.replace('#买入股票','')
     msg = new Number(msg)
     let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
    let y = x.replace(/[^0-9]/ig,'')
    y=new Number(y)
    var filename = "1.json"
    var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
    let i = ress.replace(/[^0-9]/ig,'') 
    i=new Number(i)
    let msg1 =i*msg
    if(msg1<y|msg1==y){
      y=y-msg1;let y1 = '{"num": '+y+'}'
      fs.writeFile(dirpath + "/" + filename2, JSON.stringify(y1), (err) => {
        if (err) {
          console.log(err);
        }
      })
      let name1=e.user_id+'1'
      let filename1 =  `${name1}.json`
      if (!fs.existsSync(dirpath+"/"+filename1)) {
        fs.appendFileSync(dirpath + "/" + filename1, JSON.stringify({"num": msg}))}else{
          let z = fs.readFileSync(dirpath + "/" + filename1,"utf8")
        let z1 = z.replace(/[^0-9]/ig,'')
        z1=new Number(z1)+msg
        let b = '{"num": '+z1+'}'
        b = JSON.parse(b);
      fs.writeFile(dirpath + "/" + filename1, JSON.stringify(b), (err) => {
        if (err) {
          console.log(err);
        }
      })
       
      }
        
      e.reply('你花费'+msg1+'元购买了'+msg+'股,余额还剩'+y+'元。')}else{let name1=e.user_id+'1'
      let filename1 =  `${name1}.json`
      if (!fs.existsSync(dirpath+"/"+filename1)) {
        fs.appendFileSync(dirpath + "/" + filename1, JSON.stringify({"num": 0}))
        e.reply('你的余额不足，回复 #领取救济 领点钱吧！')}else{ e.reply('你的余额不足，回复 #领取救济 领点钱吧！')}
    }
    }

  
  }
  async sell(e) {let name1=e.user_id+'1'
  let filename1 =  `${name1}.json`
  if (!fs.existsSync(dirpath+"/"+filename1)) {
    fs.appendFileSync(dirpath + "/" + filename1, JSON.stringify({"num": 0}))
    e.reply('你的手上没有股票，无法卖出')
  }else{let name = e.user_id
    let filename2 =  `${name}.json`
    let msg = e.msg.replace('#卖出股票','')
    msg = new Number(msg)
    let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
   let y = x.replace(/[^0-9]/ig,'')
   y=new Number(y)
   var filename = "1.json"
   var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
   let i = ress.replace(/[^0-9]/ig,'') 
   i=new Number(i)
   let z = fs.readFileSync(dirpath + "/" + filename1,"utf8")
   let z1 = z.replace(/[^0-9]/ig,'')
   z1=new Number(z1)
   let z11=z1-msg
   if(z11>0|z11==0){
    z11='{"num": '+z11+'}'
   z11 = JSON.parse(z11);
      fs.writeFile(dirpath + "/" + filename1, JSON.stringify(z11), (err) => {
        if (err) {
          console.log(err);
        }
      })
    let msg1=msg*i
     y=y+msg1
    let y1 = '{"num": '+y+'}'
    y1 = JSON.parse(y1);
    fs.writeFile(dirpath + "/" + filename2, JSON.stringify(y1), (err) => {
      if (err) {
        console.log(err);
      }
    })
    e.reply('你已售出股票'+msg+'股，获得了'+msg1+'元，你的余额为：'+y+'元')
  }else{e.reply('你只有'+z1+'股，无法卖出那么多，想钱想疯了？')}




  }
  

}
async lqjj(e) {let now = new Date()
  let name = e.user_id
  let filename2 =  `${name}.json`
  
  var filename = "1.json"
var ress = fs.readFileSync(dirpath + "/" + filename,"utf8")
let i = ress.replace(/[^0-9]/ig,'') 
i=new Number(i)
  jiuji= e.user_id+'%'
  tname = `${jiuji}.json`
  if (!fs.existsSync(dirpath+"/"+tname)) { 
    fs.appendFileSync(dirpath + "/" + tname, JSON.stringify({"t": now}))
    
    var o = Math.floor(Math.random() * (i * 3));
    if (o > i * 3) {
        o = i * 3;
    }else if(o<i){o=i}
    let o1 = '{"t": '+o+'}'
    o1=JSON.parse(o1)
    if (!fs.existsSync(dirpath+"/"+filename2)) {
      fs.appendFileSync(dirpath + "/" + filename2, JSON.stringify({"num": o}))
      e.reply('你已成功领取'+o+'元,你现在的余额为：'+o+'元')
    }
      else{
        let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
        let y = x.replace(/[^0-9]/ig,'')
        y=new Number(y)
        y=y+o
        let y1 = '{"num": '+y+'}'
         y1=JSON.parse(y1)
         fs.writeFile(dirpath + "/" + filename2, JSON.stringify(y1), (err) => {
          if (err) {
            console.log(err);
          }
          e.reply('你已成功领取'+o+'元,你现在的余额为：'+y+'元')
  
        })


      }
    
  
  
  }else{fs.readFile(dirpath + "/" + tname, (err, date) => {
    if (err) {
      console.log(err);
    } else {
      // 解析json
      let p = JSON.parse(date);console.log(p)
      let now1 = new Date()
      // 获取json中的time
      let now11 = '{"t": '+now1+'}'
       let time = new Date(p.t);
       let time1 = time.getTime();console.log(time1)
      let time2 = now1.getTime();console.log(time2)
      let diff = time2 - time1;
  if(diff > 24 * 60 * 60 * 1000){p.time = now; fs.writeFile(dirpath + "/" + tname, JSON.stringify(now11), (err) => {
    if (err) {
      console.log(err);
    } else {
      console.log('更新成功');
    }
  })
    var o = Math.floor(Math.random() * (i * 3));
    if (o > i * 3) {
        o = i * 3;
    }else if(o<i){o=i}
    let x = fs.readFileSync(dirpath + "/" + filename2,"utf8")
    let y = x.replace(/[^0-9]/ig,'')
    y=new Number(y)
    y=y+o
    let y1 = '{"num": '+y+'}'
     y1=JSON.parse(y1)
     fs.writeFile(dirpath + "/" + filename2, JSON.stringify(y1), (err) => {
      if (err) {
        console.log(err);
      }
    })
 e.reply('你已成功领取'+o+'元,你现在的余额为：'+y+'元')
  
  }
else{console.log(diff)
  e.reply('你今日已经领过救济了')}}})



  }


}
async t(e) {console.log(t)}
async help(e) {
  e.reply('炒股口令：\n#我的账户\n#买入股票+数字\n#卖出股票+数字\n#领取救济（每天一次）\n#查询股价（每半小时发生一次变动）\n如发现bug或运行出现问题请联系开发者')
}





















}
